package client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 *
 * @author: SJie
 * @Date: 2020/8/8 22:34
 * @description:
 *
 */
public class ByteBufUtil {
    /**
     * ByteBuf转字节
     * @param obj
     * @return
     */
    public static byte[] ByteBufToBytes(Object obj){
        ByteBuf buf = (ByteBuf) obj;
        byte[] data = new byte[buf.readableBytes()];
        buf.readBytes(data);
        return data;
    }

    /**
     * 数据转ByteBuf
     * @param data
     * @return
     */
    public static ByteBuf hexToByteBuf(int[] data){
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer();
        byte[] b = ByteHexUtil.hexToBytes(data);
        byteBuf.writeBytes(b);
        return byteBuf;
    }
}
